package com.sen.design;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class EncryptUtils {


    /**
     * 压缩
     * @param str
     * @return
     */
    public static String zip(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes(StandardCharsets.UTF_8));
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = Base64.getEncoder().encodeToString(compressed);;
        } catch (IOException ignored) {
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException ignored) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ignored) {
                }
            }
        }
        return compressedStr;
    }

    /**
     * 解压
     * @param compressedStr
     * @return
     */
    public static String unzip(String compressedStr) {

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {
            byte[] decode = Base64.getDecoder().decode(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(decode);
            zin = new ZipInputStream(in);
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString(StandardCharsets.UTF_8.name());
        } catch (IOException ignored) {
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException ignored) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ignored) {
                }
            }
        }
        return decompressed;
    }

    public static void main(String[] args) {
        String a = "UEsDBBQACAgIACuB7lIAAAAAAAAAAAAAAAABAAAAMKtWKkotUbJSMlDSUUpMTk4tLi7Jz07NA4okGVpYmhkbWVqYp6WaGJmkWBgamZgnG5kbWyQmGaRZJgM15BanAxU+65jwtGu+Ui0AUEsHCGgkFaFKAAAASwAAAA==";
        System.out.println(unzip(a));

//        System.out.println(getMD5Str("123456").equals("e10adc3949ba59abbe56e057f20f883e"));
    }
}
